home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / MacCleo / geoface / fileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  6.9 KB  |  285 lines

  1. /* ==========================================================================
  2.                                FILEIO_C
  3. =============================================================================
  4.  
  5.     FUNCTION NAMES
  6.  
  7.     read_polygon_indices        -- reads the polygon indices file.
  8.     read_polygon_line        -- read the face polyline.
  9.     read_muscles        -- reads the face muscles. 
  10.     read_expression_vectors     -- reads a vector of expressions.
  11.     add_muscle_to_face         -- add a muscle to the face.
  12.  
  13.     C SPECIFICATIONS
  14.  
  15.     read_polygon_indices    ( FileName, face ) 
  16.     read_polygon_line        ( FileName, face )
  17.     read_muscles        ( FileName, face )
  18.     read_expression_vectors    ( FileName, face )
  19.     add_muscle_to_face        ( m, face )
  20.  
  21.     DESCRIPTION
  22.     
  23.     This module is responsible for reading the face data files.  
  24.     This module comes as is with no warranties. 
  25.  
  26.     SIDE EFFECTS
  27.     Unknown.
  28.    
  29.     HISTORY
  30.     Created 16-Dec-94  Keith Waters at DEC's Cambridge Research Lab.
  31.     Modified 22-Nov-96 Sing Bing Kang (sbk@crl.dec.com)
  32.        modified function read_expression_vectors() to allocate
  33.        memory to face->expression (done once)
  34.  
  35. ============================================================================ */
  36.  
  37. #include <math.h>        /* C header for any math functions         */
  38. #include <stdio.h>        /* C header for standard I/O                 */
  39. #include <string.h>        /* For String compare                  */
  40. #include <stdlib.h>
  41. #ifndef _WIN32
  42. #include <sys/types.h>
  43. #include <sys/file.h>
  44. #endif
  45.  
  46. /* 
  47.  * from /usr/include/sys/types.h
  48.  * Just in case TRUE and FALSE are not defined
  49.  */
  50.  
  51. #ifndef TRUE
  52. #define TRUE    1
  53. #endif
  54.  
  55. #ifndef FALSE
  56. #define FALSE   0
  57. #endif
  58.  
  59.  
  60. #include "head.h"        /* local header for the face data structure  */
  61. #include "memory.h"
  62.  
  63. void add_muscle_to_face ( MUSCLE  *m , HEAD    *face );
  64.  
  65. /* ========================================================================= */  
  66. /* read_polygon_indices                                                      */
  67. /* ========================================================================= */  
  68. /*
  69. **   Read in the face data file (x,y,z)
  70. **
  71. */
  72.  
  73. void
  74. read_polygon_indices ( char *FileName, HEAD *face )
  75.  { 
  76.    FILE *InFile ;
  77.    int i, ii ;
  78.    /* 
  79.     * Check the FileName.
  80.     */
  81.    if (( InFile = fopen ( FileName, "r" )) == 0 ) {
  82.      fprintf ( stderr, "can't open input file: %s\n", FileName ) ;
  83.      exit(-1) ;
  84.    }
  85.      
  86.    fscanf ( InFile,"%d", &face->npindices ) ;
  87.  
  88.    /* 
  89.     * Allocate some memory.
  90.     */
  91.    face->indexlist = ( int * ) malloc ( face->npindices*4 * sizeof ( int )) ; 
  92.  
  93.    for( i=0, ii=0; i<face->npindices; i++, ii+=4 )
  94.      fscanf(InFile,"%d%d%d%d", 
  95.         &face->indexlist[ii],   &face->indexlist[ii+1], 
  96.         &face->indexlist[ii+2], &face->indexlist[ii+3] ) ;
  97.         
  98.    fclose( InFile ) ;
  99.    
  100.  }
  101.  
  102. /* ========================================================================= */  
  103. /* read_polygon_line                                                         */
  104. /* ========================================================================= */  
  105. /*
  106. **   Read in the face data file (x,y,z)
  107. **
  108. */
  109.  
  110. void
  111. read_polygon_line ( char *FileName, HEAD *face )
  112. {
  113.   FILE *InFile ;
  114.   int i, ii ;
  115.  
  116.    /* 
  117.     * Check the FileName.
  118.     */
  119.    if (( InFile = fopen ( FileName, "r" )) == 0 ) {
  120.      fprintf ( stderr, "can't open input file: %s\n", FileName ) ;
  121.      exit(-1) ;
  122.    }
  123.  
  124.   fscanf ( InFile, "%d", &face->npolylinenodes ) ;
  125.  
  126.   /*
  127.    * Allocate some memory.
  128.    */
  129.   face->polyline = ( float * ) malloc ( face->npolylinenodes*3 * sizeof ( float )) ; 
  130.  
  131.   for ( i=0, ii=0; i<face->npolylinenodes; i++, ii+=3 ) {
  132.     
  133.     fscanf ( InFile,"%f%f%f",
  134.         &face->polyline[ii], 
  135.         &face->polyline[ii+1], 
  136.         &face->polyline[ii+2] ) ;
  137.   }
  138.   
  139.   fclose ( InFile ) ;
  140.  
  141. }
  142.  
  143. /* =============================================================
  144.    read_muscles ( FileName, face )
  145.    ========================================================== */
  146. /*
  147. ** This function reads in the muscles.
  148. **
  149. */
  150.  
  151. void
  152. read_muscles ( char *FileName, HEAD *face )
  153. {
  154.   FILE *Infile;
  155.   int i, nm ;
  156.   MUSCLE *m ;
  157.  
  158.   /* 
  159.    * Open the file to be read.
  160.   */
  161.   if((Infile = fopen(FileName,"r")) == 0) {
  162.       fprintf(stderr,"Opening error on file:%10s\n", FileName) ;
  163.       exit(0);
  164.     }
  165.   fscanf ( Infile, "%d", &nm ) ;
  166.  
  167.   for ( i=0; i < nm; i++ ) {
  168.  
  169.       m = _new ( MUSCLE ) ;
  170.  
  171.       fscanf (Infile, "%s %f %f %f %f %f %f %f %f %f %f",
  172.           &(*m->name),
  173.           &m->head[0], &m->head[1], &m->head[2],
  174.           &m->tail[0], &m->tail[1], &m->tail[2],
  175.           &m->fs, &m->fe, &m->zone, &m->clampv ) ;
  176.  
  177.       m->active = FALSE ;
  178.       m->mstat  = 0.0 ;
  179.       
  180.       if (verbose) {
  181.       fprintf(stderr,"%s: %d\n========================\nhx: %2.2f hy: %2.2f hz: %2.2f\ntx: %2.2f ty: %2.2f tz: %2.2f\n fall start: %2.2f\n fall end: %2.2f\n zone: %2.2f\n clampv: %2.2f mstat: %2.2f\n\n",
  182.          m->name, i, 
  183.          m->head[0], 
  184.          m->head[1], 
  185.          m->head[2],
  186.          m->tail[0], 
  187.          m->tail[1],
  188.          m->tail[2],
  189.          m->fs,
  190.          m->fe,
  191.          m->zone,
  192.          m->clampv,
  193.          m->mstat ) ;
  194.       }
  195.  
  196.       add_muscle_to_face ( m, face ) ;
  197.  
  198.     }
  199.  
  200.   fclose(Infile) ;
  201. }
  202.  
  203.  
  204. /* ========================================================================= */  
  205. /* read_expression_vectors                                                   */
  206. /* ========================================================================= */  
  207. /* sbk - added allocated var - 11/22/96 */
  208.  
  209. /*
  210. **   Read in the expression vectors.
  211. */
  212. void
  213. read_expression_vectors ( char *FileName, HEAD *face )
  214. {
  215.   FILE *InFile ;
  216.   int i, k ;
  217.   EXPRESSION *e ;
  218.  
  219.    /* 
  220.     * Check the FileName.
  221.     */
  222.    if (( InFile = fopen ( FileName, "r" )) == 0 ) {
  223.      fprintf ( stderr, "can't open input file: %s\n", FileName ) ;
  224.      face->expression = NULL;
  225.      return;
  226.    }
  227.  
  228.   fscanf ( InFile, "%d", &face->nexpressions ) ;
  229. //  fprintf( stderr, "Number of expressions = %d\n", face->nexpressions ) ;
  230.  
  231.   /*
  232.    * Allocate some memory.
  233.    */
  234.   if (face->expression) {
  235.     for (i=0;i<face->nexpressions;++i)  
  236.         free (face->expression[i]);
  237.     free( face->expression);
  238.     }
  239.   face->expression = (EXPRESSION  **)malloc( face->nexpressions*
  240.                            sizeof(EXPRESSION  *) );
  241.   
  242.   for ( i=0; i<face->nexpressions; i++) {
  243.       e = face->expression[i] = _new(EXPRESSION) ;
  244.  
  245.     fscanf ( InFile, "%s\n", &(*e->name) ) ;
  246.     
  247. //    fprintf ( stderr, "%s\n", e->name ) ;
  248.     
  249.     for ( k=0; k < 17; k++) {
  250.       
  251.       fscanf ( InFile,(k==16) ? "%f\n" : "%f ",   &e->m[k]) ;
  252. //      fprintf (stderr,"%2.2f ", e->m[k] ) ;
  253.     }
  254. //    fprintf (stderr, "\n") ;
  255.   }
  256.   
  257.   fclose ( InFile ) ;
  258. }
  259.  
  260. /* =============================================================== 
  261.    add_muscle_to_face ( m, face )
  262.    =============================================================== */
  263. /*
  264. **   adds a muscle to the face muscle list.
  265. **
  266. */
  267.  
  268. void
  269. add_muscle_to_face ( MUSCLE  *m , HEAD    *face )
  270. {
  271.   int nn ;
  272.  
  273.   if(face->nmuscles == 0)
  274.       face->muscle = _new_array(MUSCLE *, 50) ;
  275.   else if(face->nmuscles % 50 == 0)
  276.       face->muscle = _resize_array(face->muscle,MUSCLE *,face->nmuscles+50) ;
  277.  
  278.   nn = face->nmuscles ;
  279.   face->muscle[nn] = m ;
  280.  
  281.   face->nmuscles++ ;
  282.  
  283. }
  284.  
  285.